<?php  
class Admin_Model_Tour extends Zend_Db_Table_Abstract
{
        protected $_name = 'tour';
        protected $_primary = 'tour_id';
        protected $_db;
        
        public function __construct(){
            $this->_db = Zend_Registry::get('db');
        }
        
        public function listTour(){
            $sql = $this->_db->select()
                            ->from($this->_name)
                            ->order('tour_id DESC');
            return $sql;
        }
        
        
        public function getTours(){
            $sql = $this->_db->select()
                            ->from($this->_name)
                            ->order('tour_id DESC');
            $result=$this->_db->fetchAll($sql);
            return $result;
        }
        
        
        public function getTourById($tour_id){
            $where = $this->getAdapter()->quoteInto('tour_id = ?', $tour_id);
            $sql = $this->_db->select()
                                ->from($this->_name)
                                ->where($where);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function checkTour($tour_name, $tour_id = ''){
			if($tour_id != ''){
				$sql = $this->_db->select()
								->from($this->_name)
								->where('tour_name = ?', $tour_name)
                                ->where('tour_id != ?', $tour_id);
				$result = $this->_db->fetchAll($sql);
				return count($result);
			}
			else{
				$sql = $this->_db->select()
								->from($this->_name)
								->where('tour_name = ?', $tour_name);
				$result = $this->_db->fetchAll($sql);
				return count($result);
			}
		}
        
        public function convertToDate($date)
        {
            $str=explode('-',$date);
           // if($str['0']<=9){$str['0']='0'.$str['0'];}
            //if($str['1']<=9){$str['1']='0'.$str['1'];}
            
            $date=array($str['2'],$str['0'],$str['1']);
            
            return implode('/',$date);
            
        }
        
        
        public function convertToMysql($date)
        {
            $date = explode("/",$date);
            $date = array($date[2], $date[1], $date[0]);
            return $n_date=implode("-", $date);
        }
        
        
        public function addTour($data){
            $sql = $this->_db->insert($this->_name, $data);
            $tour_id = $this->_db->lastInsertId($this->_name, $this->_primary);
            return $tour_id;
        }
        
        public function editTour($data, $tour_id){
            $where = $this->getAdapter()->quoteInto('tour_id = ?', $tour_id);
            $this->_db->update($this->_name, $data, $where);
        }
        
        public function deleteTour($tour_id){
            $where = $this->getAdapter()->quoteInto('tour_id = ?', $tour_id);
            $this->_db->delete($this->_name, $where);
            $index = Zend_Search_Lucene::open('files/tour');
            $hits = $index->find('tour_id:' . $tour_id);           
            foreach($hits as $hit){
                $index->delete($hit->id);
            }
            $index->commit();
            $index->optimize();
        }
        
        public function deleteTours($listId){
            foreach($listId as $value) {
                $where = $this->getAdapter()->quoteInto('tour_id = ?', $value);
                $this->_db->delete($this->_name, $where);
                $index = Zend_Search_Lucene::open('files/tour');
                $hits = $index->find('tour_id:' . $tour_id);           
                foreach($hits as $hit){
                    $index->delete($hit->id);
                }
                $index->commit();
                $index->optimize();
            }
        }
        
        public function searchTour($keyword){
            Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('utf-8');
            setlocale(LC_CTYPE, 'UTF-8');
            Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
			$index = Zend_Search_Lucene::open('files/tour');
            if(!empty($keyword)){
                $hits = $index->find($keyword);
                return $hits;
            }
		}
        
        public function saveAddItem($arrParam = null, $options = null){
            if($options['task'] == 'add')
            {
                try
                {
                    Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('UTF-8');
                    setlocale(LC_CTYPE, 'UTF-8');
                    Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
                    $index = Zend_Search_Lucene::open('files/tour');
                    Zend_Search_Lucene::setResultSetLimit(350);
                }
                catch(exception $e)
                {
                    Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('UTF-8');
                    setlocale(LC_CTYPE, 'UTF-8');
                    Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
                    $index = Zend_Search_Lucene::create('files/tour');
                }
                
                $doc = new Zend_Search_Lucene_Document();
                $doc->addField(Zend_Search_Lucene_Field::keyword('tour_id', $arrParam['tour_id']))
                    ->addField(Zend_Search_Lucene_Field::text('tour_name', $arrParam['tour_name'], 'UTF-8'))
                    ->addField(Zend_Search_Lucene_Field::text('tour_name_filter', $arrParam['tour_name_filter'], 'UTF-8'));
                    
                $index->addDocument($doc);
                $index->commit();
                $index->optimize();
            }
            
            if($options['task'] == 'edit')
            {
                try
                {
                    Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('UTF-8');
                    setlocale(LC_CTYPE, 'UTF-8');
                    Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
                    $index = Zend_Search_Lucene::open('files/tour');
                    Zend_Search_Lucene::setResultSetLimit(350);
                }
                catch(exception $e)
                {
                    Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('UTF-8');
                    setlocale(LC_CTYPE, 'UTF-8');
                    Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
                    $index = Zend_Search_Lucene::create('files/tour');
                }
                
                $hits = $index->find('tour_id:'.$arrParam['tour_id']);
                foreach($hits as $hit)
                {
                    $index->delete($hit->id);
                }
                
                $doc = new Zend_Search_Lucene_Document();
                $doc->addField(Zend_Search_Lucene_Field::keyword('tour_id', $arrParam['tour_id']))
                    ->addField(Zend_Search_Lucene_Field::text('tour_name', $arrParam['tour_name'], 'UTF-8'))
                    ->addField(Zend_Search_Lucene_Field::text('tour_name_filter', $arrParam['tour_name_filter'], 'UTF-8'));
                
                $index->addDocument($doc);
                $index->commit();
                $index->optimize();
            }
        }
        
        
        public function Eve_Similar($tour_id)
        {
             $i=0;
            $tours=$this->getTours();
            $pre_tour=$this->getTourById($tour_id);
            $simi= array();
            foreach($tours as $tour)
            {
                // Đánh giá điểm đến điểm đi
                if($pre_tour['start_place']==$tour['start_place'] )
                {
                    $start_place_point=10;
                }
                else
                {
                    $start_place_point=0;
                }
                
                
                 // Đánh giá điểm đến 
                if($pre_tour['finish_place']==$tour['finish_place'] )
                {
                    $finish_place_point=10;
                }
                else
                {
                    $finish_place_point=0;
                }
                
                
                //  Evaluate category
                if($pre_tour['cat_id']=$tour['cat_id'])
                {
                    $cat_id_point=10;
                }
                else
                {
                    $cat_id_point=0;
                }
                
                //Evaluate start date
                
                if(abs($pre_tour['start_date']-$tour['start_date']) <= 5)
                {
                    $start_date_point=5-abs($pre_tour['start_date']-$tour['start_date']);
                }
                else
                {
                    
                    $start_date_point=0;
                }
                
               
                
                //Evaluate finish date
                
                if(abs($pre_tour['finish_date']-$tour['finish_date']) <= 5)
                {
                    $finish_date_point=5-abs($pre_tour['finish_date']-$tour['finish_date']);
                }
                else
                {
                    
                    $finish_date_point=0;
                }
                //Evaluate adult price
                if( abs($pre_tour['adult_price']-$tour['adult_price'])<= 500000)
                {
                    $adult_price_point=5;
                }
                else if(abs($pre_tour['adult_price']-$tour['adult_price'])> 500000 && abs($pre_tour['adult_price']-$tour['adult_price'])<= 1000000)
                {
                    $adult_price_point=4;
                }
                else if(abs($pre_tour['adult_price']-$tour['adult_price'])> 1000000 && abs($pre_tour['adult_price']-$tour['adult_price'])<= 1500000)
                {
                    $adult_price_point=3;
                }
                else if(abs($pre_tour['adult_price']-$tour['adult_price'])> 1500000 && abs($pre_tour['adult_price']-$tour['adult_price'])<= 2000000)
                {
                    $adult_price_point=2;
                }
                else if(abs($pre_tour['adult_price']-$tour['adult_price'])> 2000000 && abs($pre_tour['adult_price']-$tour['adult_price'])<= 2500000)
                {
                    $adult_price_point=1;
                }
                else
                {
                    $adult_price_point=0;
                }
                
                //Evaluate children price
                if( abs($pre_tour['children_price']-$tour['children_price'])<= 500000)
                {
                    $children_price_point=5;
                }
                else if(abs($pre_tour['children_price']-$tour['children_price'])> 500000 && abs($pre_tour['children_price']-$tour['children_price'])<= 1000000)
                {
                    $children_price_point=4;
                }
                else if(abs($pre_tour['children_price']-$tour['children_price'])> 1000000 && abs($pre_tour['children_price']-$tour['children_price'])<= 1500000)
                {
                    $children_price_point=3;
                }
                else if(abs($pre_tour['children_price']-$tour['children_price'])> 1500000 && abs($pre_tour['children_price']-$tour['children_price'])<= 2000000)
                {
                    $children_price_point=2;
                }
                else if(abs($pre_tour['children_price']-$tour['children_price'])> 2000000 && abs($pre_tour['children_price']-$tour['children_price'])<= 2500000)
                {
                    $children_price_point=1;
                }
                else
                {
                    $children_price_point=0;
                }
                
            // Evaluate similarity of two tour
            $point=$start_place_point+$finish_place_point+$cat_id_point+$start_date_point+$finish_date_point+$adult_price_point+$children_price_point;
            $similar=$point/50;
            
            $simi[$tour['tour_id']]=$similar;
           
            
            } 
           
            return $simi;
        }
        
}

